/**
 * Method to attach events to an object
 * @version 1.0
 * @param {Object} obj Object to attach an event to.
 * @param {String} type Event type to bind the function to the object with.
 * @param {Function} fn Function to execute when the event is fired.
 */
com.bb.AddEvent = function (obj, type, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(type, fn, false);
		BB.Core.AddEvent.EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e" + type + fn] = fn;
		obj[type + fn] = function (e) {
			obj["e" + type + fn](window.event || e);
		};
		obj.attachEvent("on" + type, obj[type + fn]);
		BB.Core.AddEvent.EventCache.add(obj, type, fn);
	}
	else {
		obj["on" + type] = obj["e" + type + fn];
	}
};
/**
 * Method to cache events for later removal.
 */
com.bb.AddEvent.EventCache = (function () {
	var listEvents = [];
	
	return {
		
		listEvents : listEvents,
		/**
		 * Add an event to the event list array
		 * @param {Object} node Object that an event is attached to.
		 * @param {String} sEventName Event type bound to the object.
		 * @param {Function} fHandle Function that is executed when the event is fired.
		 */
		add : function (node, sEventName, fHandler) {
			listEvents.push(arguments);
		},
		/**
		 * Flush the event cache, remove all event listeners
		 */
		flush : function () {
			var i, item;
			for (i = listEvents.length - 1; i >= 0; i -= 1) {
				item = listEvents[i];
				if (item[0].removeEventListener) {
					item[0].removeEventListener(item[1], item[2], item[3]);
				}
				if (item[1].substring(0, 2) !== "on") {
					item[1] = "on" + item[1];
				}
				if (item[0].detachEvent) {
					item[0].detachEvent(item[1], item[2]);
				}
				item[0][item[1]] = null;
			}
		}
		
	};
	
}());
/**
 * Function prototype method to attach events to methods.
 * @param {Object} obj Object to attach an event to.
 * @param {String} type Event type to bind the function to the object with.
 */
Function.prototype.attach = function (obj, type) {
	com.bb.AddEvent(obj, type, this);
};